package org.phoenixframework.common.tools;

import org.springframework.http.HttpStatus;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 *
 * @author doublelife
 * Date: 2019/3/20
 * Time: 15:50
 */
public class ResultModel<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private int code = HttpStatus.OK.value();

    private boolean success = Boolean.TRUE;

    private String message = HttpStatus.OK.getReasonPhrase();

    private T data;

    public ResultModel() {

    }

    public ResultModel(int code, String message) {
        this.code = code;
        this.message = message;
        this.data = null;
    }

    public ResultModel(int code, boolean success, String message) {
        this.code = code;
        this.success = success;
        this.message = message;
        this.data = null;
    }

    public ResultModel(int code, boolean success, String message, T data) {
        this.code = code;
        this.success = success;
        this.message = message;
        this.data = data;
    }

    private ResultModel(boolean success) {
        this.success = success;
    }

    private ResultModel(T data) {
        this.data = data;
    }

    public ResultModel(ResultStatus resultStatus) {
        this.code = resultStatus.getCode();
        this.message = resultStatus.getMessage();
        this.data = null;
    }

    public ResultModel(ResultStatus resultStatus, T data) {
        this.code = resultStatus.getCode();
        this.message = resultStatus.getMessage();
        this.data = data;
    }

    public ResultModel(HttpStatus httpStatus) {
        this.code = httpStatus.value();
        this.message = httpStatus.getReasonPhrase();
        this.data = null;
    }

    public ResultModel(HttpStatus httpStatus, boolean success) {
        this.code = httpStatus.value();
        this.message = httpStatus.getReasonPhrase();
        this.success = success;
        this.data = null;
    }

    public ResultModel(HttpStatus httpStatus, T data) {
        this.code = httpStatus.value();
        this.message = httpStatus.getReasonPhrase();
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static ResultModel<?> ok() {
        return new ResultModel<>();
    }

    public static ResultModel<?> ok(Boolean success) {
        return new ResultModel<>(success);
    }

    public static <T> ResultModel<T> ok(T data) {
        return new ResultModel<>(data);
    }

    public static <T> ResultModel<T> ok(String message, T data) {
        return new ResultModel<>(HttpStatus.OK.value(), Boolean.TRUE, message, data);
    }

    public static ResultModel<?> error() {
        return new ResultModel<>(HttpStatus.INTERNAL_SERVER_ERROR);
    }

    public static ResultModel<?> error(boolean success) {
        return new ResultModel<>(HttpStatus.INTERNAL_SERVER_ERROR, success);
    }

    public static <T> ResultModel<T> error(T data) {
        return new ResultModel<>(HttpStatus.INTERNAL_SERVER_ERROR, data);
    }

    public static <T> ResultModel<T> error(String message, T data) {
        return new ResultModel<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), Boolean.TRUE, message, data);
    }

    public static <T> ResultModel<T> error(String message) {
        return new ResultModel<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), Boolean.TRUE, message, null);
    }

    public static ResultModel<?> map(String key, Object value) {
        HashMap<String, Object> map = new HashMap<>(16);
        map.put(key, value);
        return ResultModel.ok(map);
    }

}
